package com.wanmait.myhouse.mediatorController;

import com.wanmait.myhouse.dao.HouseDAO;
import com.wanmait.myhouse.vo.House;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("mediator")
public class MediatorShowHouse {
    @GetMapping("showHouse")
    public String amend(Model model, HttpSession session, Integer pon ,House house){
        if (house.getMediatorId() == null){
            Object o = session.getAttribute("mediator");
            Mediator mediator = (Mediator)o;
            house.setMediatorId(mediator.getId());
        }
        HouseDAO houseDAO = new HouseDAO();
        Pager pager = new Pager();
        if(pon == null){
            pon = 1;
        }
        pager.setPageNO(pon);//第一页
        pager.setPageSize(4);
        int total = houseDAO.getTotal(house.getMediatorId());
        int pageCount = total/pager.getPageSize()+(total% pager.getPageSize()==0?0:1);
        pager.setPageCount(pageCount);
        model.addAttribute("paper",pager);
        List<House> houses =  houseDAO.FindAll(house.getMediatorId() , pager);
        model.addAttribute("houseList",houses);
        return "mediator/showHouse";
    }
}
